package vn.com.bookstore.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import vn.com.bookstore.dao.BookTypeDao;
import vn.com.bookstore.domain.model.BookType;
import vn.com.bookstore.domain.model.PagingInfo;
import vn.com.bookstore.domain.model.SearchResult;
import vn.com.bookstore.model.vo.BookTypeVO;
import vn.com.bookstore.utils.DaoHelper;

@Repository
public class BookTypeDaoImpl extends DaoHelper<BookType> implements BookTypeDao{
	
	@SuppressWarnings("rawtypes")
	class BookTypeRowMapper implements RowMapper{
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			BookType bookType = new BookType();
			bookType.setId(rs.getInt("id"));
			bookType.setName(rs.getString("name"));
			bookType.setActive(rs.getBoolean("active"));
			return bookType;
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<BookType> getAllBookType() {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM book_type BT ");
		sql.append("WHERE BT.active = 1");
		Object[] parameters = new Object[]{};
		SearchResult<BookType> result = query(sql.toString(), parameters, new BookTypeRowMapper());
		if(result.getSearchResult().size() == 0) return new ArrayList<BookType>();
		return result.getSearchResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public BookType getBookTypeById(int id) {
		String sql = "SELECT * FROM book_type WHERE id = ?";
		Object[] param = new Object[]{id};
		SearchResult<BookType> result = query(sql, param, new BookTypeRowMapper());
		if(result.getSearchResult().size() == 0) return new BookType();
		return result.getSearchResult().get(0);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<BookType> getBooks(PagingInfo pagingInfo, String keyword) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM book_type WHERE active in (0, 1) ");
		if (StringUtils.isNotEmpty(keyword)) {
			sql.append("AND (");
			String[] keyStrings = keyword.split(" ");
			for (int i = 0; i < keyStrings.length; i++) {
				String s = "'%" + keyStrings[i] + "%'";
				if (i == 0) {
					sql.append(" name LIKE " + s );
				} else {
					sql.append("OR name LIKE " + s );
				}
			}
			sql.append(")");
		}
		sql.append(" ORDER BY name");
		return queryPaging(sql.toString(), null, new BookTypeRowMapper(),
				pagingInfo);
	}

	@Override
	public void deleteBooktype(int id) {
		String sql= "Update book_type set active = 0 where id = ?";
		Object[] params = new Object[] {id};
		insert(sql, params);
	}

	@Override
	public boolean isExistedBooktype(String bookName) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT count(id) FROM book_type WHERE name = '" + bookName + "'");
		int rowNum;
		try {
			rowNum = getJdbcTemplate().queryForInt(sql.toString());
		} catch (DataAccessException e) {
			return true;
		}
		if (rowNum > 0) {
			return true;
		}
		return false;
	}

	@Override
	public void addNewBookType(BookTypeVO bookTypeVO) {
		String sql= "Insert into book_type (id,name,active) Value (?,?,?)";
		Object[] params = new Object[] {bookTypeVO.getId(),bookTypeVO.getName(),bookTypeVO.isActive()};
		insert(sql, params);
	}
	
	@Override
	public void editBookType(BookTypeVO bookTypeVO) {
		String sql= "Update book_type set name = ?,active = ? Where id = ?";
		Object[] params = new Object[] {bookTypeVO.getName(),bookTypeVO.isActive(),bookTypeVO.getId()};
		insert(sql, params);
	}
}
